var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {},
  onLoad() {
    this.getUser();
  },
  onShow() {
  },
  onShareAppMessage: function() {},

  onPullDownRefresh() {

  },
  _submit(evt) {
    const uid = wx.getStorageSync('uid');
    const amount = evt.detail.value.money;
    let types = 'alipay';
    const paramsUrl = '/public/api/orders/tixian';
    if (this._validate(evt.detail.value)) {
      wx.showActionSheet({
        itemList: ['提现到支付宝', '提现到银行卡'],
        itemColor:'#333333',
        success:(res)=>{
          console.log(res.tapIndex)
          types = res.tapIndex == 1 ?  'bank' : types;
          const paramsData = {
            uid,
            amount,
            types
          }
          ajax.P({
            url:paramsUrl,
            data: paramsData,
            success:(res)=>{
              this.getUser();
            }
          })
        },
        fail(res) {
          console.log(res.errMsg)
        }
      })
    }
  },
  _validate(value) {
    const {
      money,
    } = value;
    if (!money) {
      util.tips('提现金额不能为空！');
      return false;
    }
    return true;
  },
  getUser() {
    var parmasUrl = '/public/api/user/getUser';
    var paramsData = {
      uid: wx.getStorageSync('uid')
    };
    ajax.P({
      url: parmasUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        var res = res.data;
        this.setData({
          wallet: parseFloat(res.wallet).toFixed(2),
        })
        isLoadcb && isLoadcb();
      },
      fail: (res) => { },
      unneedLoad: true
    })
  }
}));